package com.example.schoolhelp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.example.schoolhelp.R;
import com.example.schoolhelp.base.BaseActivity;
import com.example.schoolhelp.util.LogUtil;
import com.example.schoolhelp.view.MyToast;
import com.wakehao.bar.BottomNavigationBar;
import com.wakehao.bar.BottomNavigationItemWithDot;

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {
    private boolean isOpenSetting = false;
    private Animation animation;
    private NavigationView navigationView;
    private DrawerLayout drawer;
    private ViewPager viewPager;
    FloatingActionButton fab;
    private BottomNavigationBar bottomBar;
    Toolbar toolbar;
    View toolbarLayout;

    public static final int PAGE_ONE = 0;
    public static final int PAGE_TWO = 1;
    public static final int PAGE_THREE = 2;
    public static final int PAGE_FOUR = 3;
    private int curPage = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
        initViewData();
        initEvent();
    }

    private void initData() {

    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbarLayout = toolbar.findViewById(R.id.toorbar_layout_search);
        setSupportActionBar(toolbar);
        fab = (FloatingActionButton) findViewById(R.id.fab);
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        bottomBar = (BottomNavigationBar) findViewById(R.id.bottomBar);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
    }

    private void initViewData() {
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        bottomBar.showNum(2, 100);
        bottomBar.showNum(3, -1);
    }

    private void initEvent() {
        navigationView.setNavigationItemSelectedListener(this);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               startActivity(new Intent(MainActivity.this, TestActivity.class));
                if (isOpenSetting) {
                    isOpenSetting = false;
                    animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate_fab_shunshizhen90);
                    fab.startAnimation(animation);//如果已经打开，播放关闭的动画

                } else {
                    isOpenSetting = true;
                    animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate_fab_nishizhen90);
                    fab.startAnimation(animation);//如果已经关闭，播放打开的动画
                }
            }
        });
        bottomBar.setOnNavigationItemSelectedListener(new BottomNavigationBar.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull BottomNavigationItemWithDot item, int selectedPosition) {
                LogUtil.e("onNavigationItemSelected", "selectedPosition=>" + selectedPosition);
                //点击底部按钮
                if (selectedPosition == PAGE_ONE) {
                    expandFab();
                } else {
                    shrinkFab();
                }
                curPage = selectedPosition;
                invalidateOptionsMenu();//刷新顶部菜单
                return true;
            }

            @Override
            public void onNavigationItemSelectedAgain(@NonNull BottomNavigationItemWithDot item, int reSelectedPosition) {
                LogUtil.e("onNavigationItemSelectedAgain", "selectedPosition=>" + reSelectedPosition);
                //再次点击触发
            }
        });
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                LogUtil.e("onPageSelected", "position==>" + position);
                if (position == PAGE_ONE) {
                    expandFab();
                } else {
                    shrinkFab();
                }
                curPage = position;
                invalidateOptionsMenu();//刷新顶部菜单
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    /**
     * 在调用了 invalidateOptionsMenu()后触发
     * 刷新toolbar布局
     * @param menu
     * @return
     */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem item = menu.findItem(R.id.add);
        if (curPage == PAGE_ONE) {
            item.setVisible(true);
            toolbarLayout.setVisibility(View.GONE);
        } else if (curPage == PAGE_TWO) {
            item.setVisible(false);
            toolbarLayout.setVisibility(View.VISIBLE);
        } else if (curPage == PAGE_THREE) {
            item.setVisible(false);
            toolbarLayout.setVisibility(View.GONE);
        } else if (curPage == PAGE_FOUR) {
            item.setVisible(false);
            toolbarLayout.setVisibility(View.GONE);
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case R.id.add:
                MyToast.showToast(this, "添加");
                break;
            case R.id.scan:
                MyToast.showToast(this, "扫描");
                break;
        }
        return true;
    }

    /**
     * @param item
     * @return 侧滑菜单点击
     */
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

//        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


    /**
     * 收缩fab
     */
    private void shrinkFab() {
        if (fab.getVisibility() == View.VISIBLE) {
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale_fab_shrink);
//        fab.setAnimation(animation);
            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    fab.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            fab.startAnimation(animation);
        }

    }

    /**
     * 放大fab
     */
    private void expandFab() {
        if (fab.getVisibility() != View.VISIBLE) {
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale_fab_expand);
//        fab.setAnimation(animation);
            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    fab.setVisibility(View.VISIBLE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            fab.startAnimation(animation);
        }

    }
}
